﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Pager.Entity;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmTeaOtherIncome : BaseForm
    {
        public FrmTeaOtherIncome()
        {
            InitializeComponent();
        }

        public override void RefreshForm()
        {
            BindData();
        }

        private void FrmOtherIncome_Load(object sender, EventArgs e)
        {
            EnableDate(false);

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            //if (Portal.gc.HasFunction("Finance/Income") || Portal.gc.HasFunction("Finance/Pay"))
            {
                this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
                this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
                this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            }
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);

            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<TeaOtherIncome>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    FrmEditTeaOtherIncome dlg = new FrmEditTeaOtherIncome();
                    dlg.ID = row.Cells["ID"].Value.ToString();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BindData();
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            btnAddNew_Click(null, null);
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<TeaOtherIncome>.Instance.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private string BuildConditionSql()
        {
            SearchCondition condition = new SearchCondition();
            condition.AddCondition("OperateType", this.cmbOperateType.Text, SqlOperator.Like)
                .AddCondition("Project", this.txtProject.Text, SqlOperator.Like);

            if (chkUseDate.Checked)
            {
                condition.AddCondition("OperateDate", dateTimePicker1.Value.ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual, true)
                    .AddCondition("OperateDate", dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd"), SqlOperator.LessThanOrEqual, true);
            }

            return condition.BuildConditionSql(DatabaseType.SqlServer);
        }

        public void BindData()
        {
            this.cmbOperateType.Items.Clear();
            this.cmbOperateType.Items.AddRange(DictItemUtil.GetOtherIncomeType());
            
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("Project", "项目名称");
            this.winGridViewPager1.AddColumnAlias("OperateType", "款项类型");
            this.winGridViewPager1.AddColumnAlias("Unit", "单位");
            this.winGridViewPager1.AddColumnAlias("Quantity", "数量");
            this.winGridViewPager1.AddColumnAlias("Price", "单价");
            this.winGridViewPager1.AddColumnAlias("Amount", "金额");
            this.winGridViewPager1.AddColumnAlias("Operator", "操作人员");
            this.winGridViewPager1.AddColumnAlias("OperateDate", "操作时间");
            this.winGridViewPager1.AddColumnAlias("Shop_ID", "分店ID");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("LastUpdated", "更新日期");
            #endregion

            this.winGridViewPager1.PrintTitle = "茶室其他款项登记";

            string where = BuildConditionSql();
            DataTable dt = BLLFactory<TeaOtherIncome>.Instance.GetOtherIncomeReport(where);
            if (dt.Rows.Count > 0)
            {
                decimal Amount = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    Amount += Convert.ToDecimal(row["Amount"]);
                }
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                DataRow dr = dt.NewRow();
                dr["Project"] = string.Format("收支汇总：{0:C2}", Amount);
                dr["Amount"] = Amount;
                dt.Rows.Add(dr);
            }

            this.winGridViewPager1.DataSource = dt.DefaultView;
        }

        private void EnableDate(bool enable)
        {
            this.dateTimePicker1.Enabled = enable;
            this.dateTimePicker2.Enabled = enable;
        }

        private void chkUseDate_CheckedChanged(object sender, EventArgs e)
        {
            EnableDate(this.chkUseDate.Checked);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void SearchControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            FrmEditTeaOtherIncome dlg = new FrmEditTeaOtherIncome();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }
    }
}
